#!/usr/bin/env bats

load 'test_helper'
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'

setup() {
	export TEMP_DIR="$( mktemp -d )"

	cp -a "${FIXTURES_DIR}/generic" "${TEMP_DIR}"

	# Copying the common folder
	NEW_SRC="${TEMP_DIR}/generic/project/tools/common/src/main/bash"
	mkdir -p "${NEW_SRC}"
	export PAAS_TYPE="cf"
	cp "${COMMON_DIR}"/*.sh "${NEW_SRC}/"
	mkdir -p "${NEW_SRC}/projectType"
	cp "${COMMON_DIR}"/projectType/*.sh "${NEW_SRC}/projectType"
	cp -f "${FIXTURES_DIR}/pipeline-dummy.sh" "${NEW_SRC}/pipeline-cf.sh"
	cp -f "${FIXTURES_DIR}/pipeline-dummy.sh" "${NEW_SRC}/projectType/pipeline-jvm.sh"

	# Copying the concourse folder
	NEW_CONCOURSE_SRC="${TEMP_DIR}/generic/project/tools/concourse/"
	mkdir -p "${NEW_CONCOURSE_SRC}"
	cp -r "${SOURCE_DIR}" "${NEW_CONCOURSE_SRC}"

	export ROOT_FOLDER="${TEMP_DIR}/generic/project"
	export REPO_RESOURCE="repo"
	export TOOLS_RESOURCE="tools"
	export KEYVAL_RESOURCE="keyval"
	export M2_HOME="${TEMP_DIR}/generic/project/user_home"
	export GRADLE_HOME="${TEMP_DIR}/generic/project/user_home"
	export SSH_HOME="${TEMP_DIR}/generic/project/ssh"
	mkdir -p "${SSH_HOME}"
	export TEST_MODE="true"
	export SSH_AGENT_BIN="stubbed-ssh-agent"
	export TMPDIR="${TEMP_DIR}/generic/project/tmp"
	mkdir -p "${TMPDIR}"
}

teardown() {
	rm -rf "${TEMP_DIR}"
}

function stubbed-ssh-agent() {
	echo "echo 'foo'"
}

export -f stubbed-ssh-agent

function retrieveAppName() {
	echo "foo"
}

export -f retrieveAppName

@test "should source pipeline.sh" {
	LANGUAGE_TYPE="jvm"
	PROJECT_TYPE="maven"
	source "${TASKS_DIR}/pipeline.sh"

	assert_success
	assert_equal "${PROJECT_SETUP}" "SINGLE_REPO"
	assert [ -e "${TEMP_DIR}/generic/project/user_home/settings.xml" ]
}
